import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;

public class TestBinaryTree {
   public static class TreeNode{
       public int val1;
       public char val;
       public TreeNode left;
       public TreeNode right;
       public TreeNode(char val) {
           this.val = val;
       }
       public TreeNode(int val) {
           this.val1 = val;
       }
   }
   public TreeNode root;
   public TreeNode createTree() {
       TreeNode A = new TreeNode('A');
       TreeNode B = new TreeNode('B');
       TreeNode C = new TreeNode('C');
       TreeNode D = new TreeNode('D');
       TreeNode E = new TreeNode('E');
       TreeNode F = new TreeNode('F');
       TreeNode G = new TreeNode('G');
       TreeNode H = new TreeNode('H');
       A.left = B;
       A.right = C;
       B.left = D;
       B.right = E;
       C.left = F;
       C.right = G;
       E.right = H;
       this.root = A;
       return A;
   }
   public void preorder(TreeNode root) {
       if(root == null) {
           return;
       }
       System.out.print(root.val+" ");
       preorder(root.left);
       preorder(root.right);
   }
   public void inorder(TreeNode root) {
       if(root == null) {
           return;
       }
       inorder(root.left);
       System.out.print(root.val+" ");
       inorder(root.right);
   }
   public void postOrder(TreeNode root) {
       if(root == null) {
           return;
       }
       postOrder(root.left);
       postOrder(root.right);
       System.out.print(root.val+" ");
   }
   public void func(TreeNode root) {                     //层序遍历
       Queue<TreeNode> queue = new ArrayDeque<>();
       queue.offer(root);
       while(!queue.isEmpty()) {
           TreeNode node = queue.poll();
           System.out.print(node.val+" ");
           if(node.left != null) queue.offer(node.left);
           if(node.right != null) queue.offer(node.right);
       }
   }
    /**
     * 找节点个数
     * @param root
     * @return
     */
   public int size(TreeNode root) {
       if(root == null) {
           return 0;
       }
       return size(root.left)+size(root.right)+1;
   }
   public static int count;
   public int size1(TreeNode root) {
       if(root == null) {
           return 0;
       }
       count++;
       size1(root.left);
       size1(root.right);
       return count;
   }
    /**
     * 找叶子节点个数
     */
    int getLeafSize2(TreeNode root) {
        if(root == null) {
            return 0;
        }else if(root.left == null && root.right == null) {
            return 1;
        }else {
            return getLeafSize2(root.left)+getLeafSize2(root.left);  //左边叶子节点数加右边叶子节点个数
        }
    }
    public static int leafSize = 0;
    public void getLeafSize1(TreeNode root){
        if(root == null) {
            return;
        }else if(root.left == null && root.right == null) {
            leafSize++;
        }else {
            getLeafSize1(root.left);
            getLeafSize1(root.right);
        }
    }

    /**
     * 求第k层节点个数
     * @param root
     * @param k
     * @return
     */
    public int getKLevelSize(TreeNode root,int k){
        if(root == null) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        return getKLevelSize(root.left,k-1)+getKLevelSize(root.right,k-1);
    }

    /**
     * 求树的高度
     * @param root
     * @return
     */
    int getHeight(TreeNode root){
        if(root == null) {
            return 0;
        }
        return Math.max(getHeight(root.left),getHeight(root.right))+1;
    }

    /**
     * 查找节点是否存在
     * @param root
     * @param val
     * @return
     */
    TreeNode find(TreeNode root, int val) {
        if(root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }
        TreeNode leftTree = find(root.left,val);
        if(leftTree != null) {
            return leftTree;
        }
        TreeNode rightTree = find(root.right,val);
        if(rightTree != null) {
            return rightTree;
        }
        return null;
    }
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q == null) return true;
        if(p != null && q != null && p.val == q.val) {
            return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
        }
        return false;
    }
    public boolean isSubTree(TreeNode root,TreeNode subRoot) {
        if (root == null || subRoot == null) return false;
        if (isSameTree(root, subRoot)) {
            return true;
        }
        if (isSubTree(root.left, subRoot)) return true;
        if (isSubTree(root.left, subRoot)) return true;
        return false;

    }

}
